define(['./../initial', './../service/symbolFactory'], function () {
    var app = angular.module('App');
    app.factory('ProjectMap', ['symbolFactory', '$timeout', '$q', '$rootScope',
        function (symbolFactory, $timeout, $q, $rootScope) {
            var ProjectMap = L.Map.extend({
                curPointMarksLayer: null, //存放当前监测点marker的layer
                curPointTxtLayer: null, //存放当前监测点Label的layer
                remarkLayer: null, //存放标注信息的layer
                geoJsonLayer: null, //存放布点图信息
                curPtNameHoverTime: 0, //记录用户在图表上悬停的时刻,当停留时间大于800ms时,就触发****事件
                autoSurveyMode: false,
                autoSurveyPrjName: '',
                autoSurveyControl: null,
                layerControl: null,
                overlays: null,
                initialize: function (divId, options) { //重载构造函数
                    L.Map.prototype.initialize.call(this, divId, options);
                }
            });

            /** 清除当前渲染的测点 */
            ProjectMap.prototype.clearPointsLayer = function () {
                if (this.curPointMarksLayer) {
                    this.curPointMarksLayer.clearLayers();
                }
                if (this.curPointTxtLayer) {
                    this.curPointTxtLayer.clearLayers();
                }
            };
            
            /** 地图缩放到某个点的promise */
            ProjectMap.prototype.zoomToPoint = function (point, zoom) {
                var defer = $q.defer();
                zoom = zoom || this.getZoom();
                this.setView(point, zoom);
                $timeout(function () {
                    defer.resolve();
                }, 600);
                return defer.promise;
            }

            /** 加载单个图层 */
            ProjectMap.prototype.loadEachLayer = function (layer) {
                var map = this;
                var jsonLayer = L.geoJson(layer.layer, {
                    onEachFeature: function (feature, layer) {
                        if (feature.properties.Remark) {
                            var txt = feature.properties.Remark;
                            if (txt.length === 0) return;
                            var center;
                            try {
                                if (feature.geometry.type === "Point") {
                                    center = [feature.geometry.coordinates[1], feature.geometry.coordinates[0]];
                                } else {
                                    center = L.bounds(feature.geometry.coordinates[0]).getCenter();
                                    center = [center.y, center.x];
                                }
                                var labelIcon = L.divIcon({
                                    'html': txt,
                                    'className': 'div-label',
                                    'iconSize': L.point(20, 8)
                                });
                                map.remarkLayer.addLayer(L.marker(center, {
                                    icon: labelIcon
                                }));
                            } catch (error) {
                                return;
                            }
                        }
                    },
                    style: symbolFactory.Factory,
                    pointToLayer: function (feature, latlng) {
                        return L.circleMarker(latlng, symbolFactory.PointFactory(feature));
                    }
                });
                map.geoJsonLayer.addLayer(jsonLayer);
            };

            /** 显示loading的进度,假的,只是增加用户体验,实际是用css的animation控制 */
            ProjectMap.prototype.showLoadingBar = function () {
                $('#' + this.getContainer().id).addClass('loading');
            };
            /** 取消loading进度显示 */
            ProjectMap.prototype.hideLoadingBar = function () {
                $('#' + this.getContainer().id).removeClass('loading');
            };

            /** 加载所有的Layer */
            ProjectMap.prototype.loadProjectLayers = function (layers) {
                var map = this;
                map.curPointMarksLayer = L.featureGroup().addTo(map);
                map.curPointTxtLayer = L.featureGroup().addTo(map);
                map.remarkLayer = L.featureGroup().addTo(map);
                map.geoJsonLayer = L.featureGroup().addTo(map);
                layers.forEach(function (layer) {
                    map.loadEachLayer(layer);
                })
                map.curPointMarksLayer.clearLayers();
                map.curPointTxtLayer.clearLayers();
                $timeout(function () {
                    map.hideLoadingBar();
                }, 0);
            };

            return ProjectMap;
        }
    ]);
});
